Perl & CGI Tutorial

Return to Table of Contents.

Introduction

What is perl?

Perl stands for Practical Extraction and Report Language. Perl has traditionally been used within Unix for automating administration tasks. It is particularly suited to these tasks because programs which do string manipulation can be written quickly (lazily) and without the overhead of memory management etc. Perl is particularly good at pattern matching (ala sed/awk) string manipulation and hashing functions. As sometimes happens, the strengths of Perl have matched the needs of an emerging programming domain. The growth of the World Wide Web has produced a stateless(?) client-server architecture where Perl is particularly useful.

Web server developers allow users to modify the functionality of their web servers by changing configuration files or even adding completely new functions to the server. A variety of interfaces have been developed to extend server functionality including APIs, plug-in modules and an interface known as the Common Gateway Interface (CGI). Over the past few years, people have developed a tremendous range of applications using this interface and these applications have helped catapault the WWW to it's current level of popularity.

Programs which work within the context of CGI can be written in any language (often in shell scripts, C or C++, but increasingly in database languages, lisp, tcl, Java etc.), but Perl remains one of the primary choices for programming in this context. This tutorial focuses on using Perl to program in the CGI environment both because this is an effective combination of language and domain, but also because domain and language ground one another. Talking about CGI without also talking about a programming language is vacuous. And the reverse is frankly too large in scope to be useful.

What is this CGI thing?

The functionality of a web server can be extended through the use of the CGI interface. (CGI was originally created on Unix systems. Therefore an understanding of Unix is very useful in understanding CGI. This tutorial assumes a general knowledge of Unix, of the Web and of HTTP servers. This may be overly optimistic... Oh well.) When a web server receives a request for a resource, it typically looks for cooresponding file on the server's file system. The server determines some various meta-information, and then sends both the meta-info and content of the file to the requestor.

Sometimes, the requested resource is interpereted as a CGI resource. When this happens, the file is not read but executed. Parameters are passed through the command-line interface, standard input and through environment variables. Output from the executable is passed along to the server and then sent out to the requestor. The client request is essentially generated by an executable program.

Generally, the output of a CGI executable is text in HTML. However, the output can be plain text, an image or any standard (or proprietary) MIME type. For the most part, this tutorial focuses on the problem of HTML output and lets the reader generalize to other media types.

How to run a perl program?

Perl is an interpereted language, so whenever a perl program is run, it must be run using the interpretor.

Example

perl filename

should execute the perl instructions in the file.

Users are often insulated from knowing this detail. Most shells support the following convention -- if the first line of an executable text file starts with #!, than the remainder of the line is used as the interpretor for the file. Therefore, all the perl program examples given here will follow the following form.

Example

#!/usr/local/bin/perl
print "Hello World\n";

Comments

Comments are marked by the pound character. Text to the right of the pound character is not interpreted within perl.

Example

#!/usr/local/bin/perl
# This is a Comment that will not be interpreted
print "Hi\n"; # This is also comment

Output

Hi

Variable Declarations

Variables do not need to be declared in perl. Space is allocated for variables when you first use them, and type can be determined by the first character of a variable. Here is a table listing the different types of variables as an demonstration:

FIZ
filehandle
$FIZ
scalar
@FIZ
array/list
%FIZ
associative array
&FIZ
subroutine
*FIZ
All variables named FIZ

Scalar Variable Type

The scalar is the primative data type in perl. The scalar is not limited in terms of size, and all the basic data types (int, float, char, string) can be put in a scalar.

Example

#!/usr/local/bin/perl
$String = "Watch this";
$Num = 6;
$Result = $Num * 2;
print "$String $Num * 2 = $Result\n";

Output

Watch this 6 * 2 = 12
Note: the values of the scalars are substituted for the scalars within double quotes. No interpretation is done within single quotes.

Example

#!/usr/local/bin/perl
$This = "Watch";
print "$This\n";
print '$This\n';

Output

Watch
$This

Scalar Operators

A table of scalar operators is available.

Default Scalar

There are many special scalars. One of these is the default scalar. The default scalar may be represented as $_, but usually it is not written when it is used. This scalar is assigned to where no other assignment is made (data input or pattern matching without an L-value). $_ also serves as a default argument when no other argument is listed.

Example

#!/usr/local/bin/perl
while(<STDIN>) {print;}

Input

BlahBlah
Blah

Output

BlahBlah
Blah

Arrays and Lists

Arrays and Lists are the same thing. Unlike other languages, arrays and lists are flat. In other words, perl does not support multi-dimensional arrays or lists within lists.

Arrays are represented by prefixing them with the @ symbol. The array MyList is written @MyList.

Assigning arrays

@MyList = ("hi", "there");
@AList  = ();			# Empty list, $#BList = -1
@BList  = (1, 2, @AList, 3);	# @CList = (1, 2, 3)
@CList  = (1, 2, @MyList, 3); 	# @AList = (1, 2, "hi", "there", 3)
The size of the array may grow and shrink by adding and removing elements from the array.

Referencing Arrays

$List[n]
Returns element n from array @List.
$#List
The maximum subscript currently in @List.
$List
The number of elements in @List (usually $#List + 1).

Functions on arrays

push(@abz, $z);
Add $z to the beginning of list @abz.
$z = pop(@abz);
remove first element from @abz place value in $z.
$z = splice(@abz1, @abz2, $a, $b);
All purpose list manipulator.
foreach $z(@abz) BLOCK
Execute BLOCK for each value in @abz. The list elements are copied into $z.
@bca = sort @abz
Returns the elements of @abz sorted in string order.

Associative Arrays

Associative arrays are similar to simple arrays except that instead of referencing elements linearly (by numbering them), elements are referenced by arbitrary scalars.

Assigning associative arrays

%MyList = (	"dog", "big",
		"cat", "small",
		"horse", "huge",
		"guppy", "miniscule");
After this assignment, $MyList{"dog"} returns "big".
$MyList{"whale"} = "humongous"
This assignment adds the association "whale"=> "humongous" to the associative array %MyList.

Referencing associative arrays

$List{$z}
Element referenced by $z from array %List.
Note that this method is similar to standard arrays except that curly braces are used instead of square braces.

Functions on arrays

@abz = keys (%MyList)
Return a list of keys in %MyList to @abz. In this case, the result could be @abz = ("dog", "cat", "horse", "guppy", "whale"). Note, You are not guarenteed anything about the order of this list. The keys are kept in a hash table, so data will likely be returned in a different order from which they were entered.

File handles

Conditional and Loop Statements

Subroutines

Principled Examples

Real Examples

Perl Pointers

Last modified: November 14, 1994 by David Carlson )